home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / zcrypt10.zip / ZIPCLOAK.C < prev   
C/C++ Source or Header  |  1991-11-08  |  9KB  |  298 lines

  1. /*
  2.    This code is not copyrighted and is put in the public domain. It
  3.    was originally written in Europe and can be freely distributed from
  4.    any country except the U.S.A. If this code is imported in the U.S.A,
  5.    it cannot be re-exported from the U.S.A to another country. (This
  6.    restriction might seem curious but this is what US law requires.)
  7.  */
  8.  
  9. #define UTIL
  10. #include "revision.h"
  11. #include "zip.h"
  12. #include <signal.h>
  13.  
  14. #define PWLEN 80        /* Input buffer size for reading encryption key */
  15.  
  16.  
  17. /* Temporary zip file name and file pointer */
  18. local char *tempzip;
  19. local FILE *tempzf;
  20.  
  21.  
  22. /* Local functions */
  23. local void err OF((int code, char *msg));
  24. local void handler OF((int sig));
  25. local void license OF((void));
  26. local void help OF((void));
  27. void main OF((int argc, char **argv));
  28.  
  29.  
  30.  
  31. /***********************************************************************
  32.  * Issue a message for the error, clean up files and memory, and exit.
  33.  */
  34. local void err(code, msg)
  35.     int code;               /* error code from the ZE_ class */
  36.     char *msg;              /* message about how it happened */
  37. {
  38.     if (PERR(code)) perror("zipcloak error");
  39.     fprintf(stderr, "zipcloak error: %s (%s)\n", errors[code-1], msg);
  40.     if (tempzf != NULL) fclose(tempzf);
  41.     if (tempzip != NULL) {
  42.     destroy(tempzip);
  43.     free((voidp *)tempzip);
  44.     }
  45.     if (zipfile != NULL) free((voidp *)zipfile);
  46. #ifdef VMS
  47.     exit(0);
  48. #else
  49.     exit(code);
  50. #endif
  51. }
  52.  
  53.  
  54. /***********************************************************************
  55.  * Upon getting a user interrupt, turn echo back on for tty and abort
  56.  * cleanly using err().
  57.  */
  58. local void handler(sig)
  59.     int sig;                  /* signal number (ignored) */
  60. {
  61. #ifndef MSVMS
  62.     echon();
  63.     putc('\n', stderr);
  64. #endif
  65.     err(ZE_ABORT +sig-sig, "aborting");
  66.     /* dummy usage of sig to avoid compiler warnings */
  67. }
  68.  
  69.  
  70. /***********************************************************************
  71.  * Print a warning message to stderr and return.
  72.  */
  73. void warn(msg1, msg2)
  74.     char *msg1, *msg2;        /* message strings juxtaposed in output */
  75. {
  76.     fprintf(stderr, "zipcloak warning: %s%s\n", msg1, msg2);
  77. }
  78.  
  79.  
  80. static char *public[] = {
  81. "The encryption code of this program is not copyrighted and is put in the",
  82. "public domain. It was originally written in Europe and can be freely",
  83. "distributed from any country except the U.S.A. If this program is imported",
  84. "in the U.S.A, it cannot be re-exported from the U.S.A to another country.",
  85. "The copyright notice of the zip program applies to the rest of the code."
  86. };
  87.  
  88. /***********************************************************************
  89.  * Print license information to stdout.
  90.  */
  91. local void license()
  92. {
  93.     extent i;             /* counter for copyright array */
  94.  
  95.     for (i = 0; i < sizeof(public)/sizeof(char *); i++) {
  96.         puts(public[i]);
  97.     }
  98.     for (i = 0; i < sizeof(disclaimer)/sizeof(char *); i++) {
  99.         puts(disclaimer[i]);
  100.     }
  101. }
  102.  
  103.  
  104. static char *help_info[] = {
  105. "",
  106. "ZipCloak %d.%d (%s)",
  107. "Usage:  zipcloak [-d] [-b path] zipfile",
  108. "  the default action is to encrypt all unencrypted entries in the zip file",
  109. "  -d   decrypt--decrypt encrypted entries (copy if given wrong password)",
  110. "  -b   use \"path\" for the temporary zip file",
  111. "  -h   show this help               -l   show software license"
  112.   };
  113.  
  114. /***********************************************************************
  115.  * Print help (along with license info) to stdout.
  116.  */
  117. local void help()
  118. {
  119.     extent i;             /* counter for help array */
  120.  
  121.     for (i = 0; i < sizeof(public)/sizeof(char *); i++) {
  122.         puts(public[i]);
  123.     }
  124.     for (i = 0; i < sizeof(help_info)/sizeof(char *); i++) {
  125.         printf(help_info[i], REVISION / 10, REVISION % 10, REVDATE);
  126.         putchar('\n');
  127.     }
  128. }
  129.  
  130. /***********************************************************************
  131.  * Encrypt or decrypt all of the entries in a zip file.  See the command
  132.  * help in help() above.
  133.  */
  134.  
  135. void main(argc, argv)
  136.     int argc;             /* number of tokens in command line */
  137.     char **argv;          /* command line tokens */
  138. {
  139.     int attr;             /* attributes of zip file */
  140.     ulg start_offset;     /* start of central directory */
  141.     int decrypt;          /* decryption flag */
  142.     int temp_path;        /* 1 if next argument is path for temp files */
  143.     char passwd[PWLEN+1]; /* password for encryption or decryption */
  144.     char *q;              /* steps through option arguments */
  145.     int r;                /* arg counter */
  146.     int res;              /* result code */
  147.     ulg length;           /* length of central directory */
  148.     FILE *inzip, *outzip; /* input and output zip files */
  149.     struct zlist far *z;  /* steps through zfiles linked list */
  150.  
  151.  
  152.     /* If no args, show help */
  153.     if (argc == 1) {
  154.     help();
  155.     exit(0);
  156.     }
  157.  
  158.     /* Go through args */
  159.     zipfile = tempzip = NULL;
  160.     tempzf = NULL;
  161.     signal(SIGINT, handler);
  162.     signal(SIGTERM, handler);
  163.     temp_path = decrypt = 0;
  164.     for (r = 1; r < argc; r++) {
  165.     if (*argv[r] == '-') {
  166.         if (!argv[r][1]) err(ZE_PARMS, "zip file cannot be stdin");
  167.         for (q = argv[r]+1; *q; q++) {
  168.         switch(*q) {
  169.         case 'b':   /* Specify path for temporary file */
  170.             if (temp_path) {
  171.             err(ZE_PARMS, "use -b before zip file name");
  172.             }
  173.             temp_path = 1;          /* Next non-option is path */
  174.             break;
  175.         case 'd':
  176.             decrypt = 1;  break;
  177.         case 'h':   /* Show help */
  178.             help();
  179.             exit(0);
  180.         case 'l':   /* Show copyright and disclaimer */
  181.             license();
  182.             exit(0);
  183.         default:
  184.             err(ZE_PARMS, "unknown option");
  185.         } /* switch */
  186.         } /* for */
  187.  
  188.     } else if (temp_path == 0) {
  189.         if (zipfile != NULL) {
  190.         err(ZE_PARMS, "can only specify one zip file");
  191.  
  192.         } else if ((zipfile = ziptyp(argv[r])) == NULL) {
  193.         err(ZE_MEM, "was processing arguments");
  194.         }
  195.     } else {
  196.         tempath = argv[r];
  197.         temp_path = 0;
  198.     } /* if */
  199.     } /* for */
  200.  
  201.     if (zipfile == NULL) err(ZE_PARMS, "need to specify zip file");
  202.  
  203.     /* Read zip file */
  204.     if ((res = readzipfile()) != ZE_OK) err(res, zipfile);
  205.     if (zfiles == NULL) err(ZE_NAME, zipfile);
  206.  
  207.     /* Check for something to do */
  208.     for (z = zfiles; z != NULL; z = z->nxt) {
  209.     if (decrypt ? z->flg & 1 : !(z->flg & 1)) break;
  210.     }
  211.     if (z == NULL) {
  212.     err(ZE_NONE, decrypt ? "no encrypted files"
  213.                        : "all files encrypted already");
  214.     }
  215.  
  216.     /* Before we get carried away, make sure zip file is writeable */
  217.     if ((inzip = fopen(zipfile, "a")) == NULL) err(ZE_CREAT, zipfile);
  218.     fclose(inzip);
  219.     attr = getfileattr(zipfile);
  220.  
  221.     /* Open output zip file for writing */
  222.     if ((tempzf = outzip = fopen(tempzip = tempname('Z'), FOPW)) == NULL) {
  223.     err(ZE_TEMP, tempzip);
  224.     }
  225.  
  226.     /* Get password */
  227.     if (getp("Enter password: ", passwd, PWLEN+1) == NULL) {
  228.         err(ZE_PARMS, "stderr is not a tty (you may never see this message!)");
  229.     }
  230.  
  231.     /* Open input zip file again, copy preamble if any */
  232.     if ((inzip = fopen(zipfile, FOPR)) == NULL) err(ZE_NAME, zipfile);
  233.  
  234.     if (zipbeg && (res = fcopy(inzip, outzip, zipbeg)) != ZE_OK) {
  235.     err(res, res == ZE_TEMP ? tempzip : zipfile);
  236.     }
  237.     /* Go through local entries, copying, encrypting, or decrypting */
  238.     for (z = zfiles; z != NULL; z = z->nxt) {
  239.     if (decrypt && (z->flg & 1)) {
  240.         printf("decrypting %s", z->zname);
  241.         fflush(stdout);
  242.         if ((res = zipbare(z, inzip, outzip, passwd)) != ZE_OK) {
  243.         if (res != ZE_MISS) err(res, "was decrypting an entry");
  244.         printf(" (wrong password--just copying)");
  245.         }
  246.         putchar('\n');
  247.  
  248.     } else if ((!decrypt) && !(z->flg & 1)) {
  249.         printf("encrypting %s\n", z->zname);
  250.         fflush(stdout);
  251.         if ((res = zipcloak(z, inzip, outzip, passwd)) != ZE_OK) {
  252.         err(res, "was encrypting an entry");
  253.         }
  254.     } else {
  255.         printf("copying %s\n", z->zname);
  256.         fflush(stdout);
  257.         if ((res = zipcopy(z, inzip, outzip)) != ZE_OK) {
  258.         err(res, "was copying an entry");
  259.         }
  260.     } /* if */
  261.     } /* for */
  262.     fclose(inzip);
  263.  
  264.     /* Write central directory and end of central directory */
  265.  
  266.     /* get start of central */
  267.     if ((start_offset = ftell(outzip)) == -1L) err(ZE_TEMP, tempzip);
  268.  
  269.     for (z = zfiles; z != NULL; z = z->nxt) {
  270.     if ((res = putcentral(z, outzip)) != ZE_OK) err(res, tempzip);
  271.     }
  272.  
  273.     /* get end of central */
  274.     if ((length = ftell(outzip)) == -1L) err(ZE_TEMP, tempzip);
  275.  
  276.     length -= start_offset;               /* compute length of central */
  277.     if ((res = putend((int)zcount, length, start_offset, zcomlen,
  278.                        zcomment, outzip)) != ZE_OK) {
  279.     err(res, tempzip);
  280.     }
  281.     tempzf = NULL;
  282.     if (fclose(outzip)) err(ZE_TEMP, tempzip);
  283.     if ((res = replace(zipfile, tempzip)) != ZE_OK) {
  284.     warn("new zip file left as: ", tempzip);
  285.     free((voidp *)tempzip);
  286.     tempzip = NULL;
  287.     err(res, "was replacing the original zip file");
  288.     }
  289.     free((voidp *)tempzip);
  290.     tempzip = NULL;
  291.     setfileattr(zipfile, attr);
  292.     free((voidp *)zipfile);
  293.     zipfile = NULL;
  294.  
  295.     /* Done! */
  296.     exit(0);
  297. }
  298.